<?php 
## include required files
/*******************************/
require_once '../model/leads.php';
require_once '../model/state.php';
require_once '../model/trade.php';
require_once '../model/budget.php';
require_once '../model/membershipplan.php';


/*******************************/
## Create Objects
/*******************************/

$objLeads = new Model_Leads();
$objState = new Model_State();
$objjob = new Model_TRADE();
$objPlan = new Model_PLAN();
$budgetObj = new Model_Budget();
/*******************************/

//ini_set('max_execution_time', 400);
//error_reporting(E_ALL);
//ini_set("dispaly_errors","1");

if(isset($_POST['addcity_btn'])){
		
	$fileName	= DOC_ROOT.'/admin/manageleads/temporary/'.$_FILES['importfile']['name'];  
	$tmpName = $_FILES['importfile']['tmp_name'];	 

	$fileSize   = ($_FILES["importfile"]["size"]);
	//echo $fileSize;exit;
	
	if($fileSize >2000000)
	{
		$_SESSION['msg'] = "<div class='error_msg'><span>Please upload file size upto 2MB!</span></div>";
				header('location:'.SITE_URL.'/admin/home.php?q=uploadcode');
				exit;
		
	}else{
		move_uploaded_file($_FILES["importfile"]["tmp_name"],$fileName);	
		chmod($fileName,0777); 

		require_once 'Excel/reader.php';
		$data = new Spreadsheet_Excel_Reader();
		
		$data->read($fileName);
		
		$countRejected=array();
		$countAccepted=array();
		$countAlreadyExists = 0;
		$dataArray=$data->sheets[0]['cells'];  
	
		//echo "<pre>";print_r($dataArray);exit;
		$j=0;
		foreach($dataArray as $key=>$value)
		{  	
		//echo "<pre>"; print_r($value);exit;
			if($key!=1)
			{
				$getstatedetails=$objState->checkStateName($value[1]);
				//$getsuburbdetails=$objState->checkSuburbNameAndPostCode($value[2],$value[3]);
				$getjobdetails=$objjob->checkJobTypeName($value[2]);
				$getplandetail = $objPlan->Checkplanname($value[5]);
				
				$bugetexplode= @explode("Under $",$value[3]);
								
				$budgetstr ='';
				$budgetstr = trim($bugetexplode[1]); 
				
				$budgetdetail = $budgetObj->checkBudgetName($budgetstr);
				//echo "<pre>";print_r($getleadsdetail);exit;
				if($getstatedetails['state_id'] !='' && $getjobdetails['id']!='' /*&& $getsuburbdetails['id']!=''*/ && $getplandetail['plan_id']!='' && $budgetdetail['budget_id']!='')
				{
						
					$leadsAddArray['budget']   = $budgetdetail['budget_id'];						
					$leadsAddArray['jobtype']   = $getjobdetails['id'];		
					$leadsAddArray['region_id']      = $getstatedetails['state_id'];

					$explode= @explode("$",$value[4]);
					//echo "<pre>";print_r($explode);exit;
					$pricestr =$explode[1];
					//echo $pricestr = number_format(trim($explode[1]),2); die();
					
					
					$leadsAddArray['price']      = $pricestr;					
					$leadsAddArray['plan_id']      = $getplandetail['plan_id'];
					$suburb_id= $getsuburbdetails['id'];
					//$leadsAddArray['suburb_id']      = $suburb_id;
					//$leadsAddArray['postalcode']      = $value[3];
					$leadsAddArray['status']      = '1';
					$leadsAddArray['dateadded']      = date('Y-d-m H:i:s');
					
					$getleadsdetail = $objLeads->getLeadsExits($getstatedetails['state_id'],$getjobdetails['id'],$budgetdetail['budget_id'],$pricestr);
					if($getleadsdetail['lead_id']=='')
					{
						$objLeads->addLeads($leadsAddArray);
						$j++;
					}
					else
					{
						$countRejected[$key]=array();
						$countRejected[$key][0]=$value[1];
						$countRejected[$key][1]=$value[2];
						$countRejected[$key][2]=$value[3];
						$countRejected[$key][3]=$value[4];
						$countRejected[$key][4]=$value[5];
						$countRejected[$key][5]=$value[6];	
					}
				}
				/*else
				{
					if($getstatedetails['state_id'] =='')
					{
						$getstatemoredetails=$objState->checkStateNameByName(trim($value[1]));
						
						if($getstatemoredetails['state_id'] == '')
						{
							$stateAddArray['country_id']   = 226;							
							$stateAddArray['state_name']      = trim($value[1]);
							$stateAddArray['state_status']      = '1';
							$newstateid = $objState->addState($stateAddArray);
						}
						else
							$newstateid = $getstatemoredetails['state_id']; 	
					}
					else
						$newstateid = $getstatedetails['state_id']; 
						
					if($getjobdetails['id']=='')
					{
						$tradeArray['trade_category']   = trim($value[4]);
						$tradeArray['parent_id']   = 59;
						$tradeArray['status']   = 1;
						$newjobtypid = $objjob->addTradeCategory($tradeArray);
					}	
					else
						$newjobtypid = $getjobdetails['id'];
						
					/*$bugetexplode= @explode("To",$value[5]);
					$leftbugetexplode= @explode("$",$bugetexplode[0]);
					$rightbugetexplode= @explode("$",$bugetexplode[1]);
					
					$budgetstr ='';
					$budgetstr = trim($leftbugetexplode[1]).'-'.trim($rightbugetexplode[1]);
					$leadsAddArray['budget']   = $budgetstr;	
					
					$bugetexplode= @explode("Under $",$value[5]);
					
					$budgetstr ='';
					$budgetstr = trim($bugetexplode[1]);
					
					$budgetdetail = $budgetObj->checkBudgetName($budgetstr);
					$leadsAddArray['budget']   = $budgetdetail['budget_id'];

					
						
					$leadsAddArray['jobtype']   = $newjobtypid;		
					$leadsAddArray['region_id']      = $newstateid;

					$explode= @explode("$",$value[6]);
					$pricestr ='';
					$pricestr = number_format(trim($explode[1]),2);
					
					$leadsAddArray['price']      = $pricestr;
					
					$leadsAddArray['plan_id']      = $getplandetail['plan_id'];
					
					$suburb_id= $getsuburbdetails['id'];
					$leadsAddArray['suburb_id']      = $suburb_id;
					$leadsAddArray['postalcode']      = $value[3];
					
					$leadsAddArray['status']      = '1';
					$leadsAddArray['dateadded']      = date('Y-d-m H:i:s');	
					
					$objLeads->addLeads($leadsAddArray);
					$j++;
				}  */
			}
			
		}
		if(file_exists($fileName))
			unlink($fileName);
		
		//$show=false;
		if($j != '0')
			$_SESSION['msg'] = "<div class='success_msg'><span>Lead Price Added Successfully!</span></div>";
		else
			$_SESSION['msg'] = "<div class='success_msg'><span>All Lead Price Already Exits!</span></div>";
				
		header('location:'.SITE_URL.'/admin/home.php?q=leads');
		exit;
	}
}	

## Assign success or error msg to smarty variable and unset session variable
if(trim($_SESSION['msg']) != '') {
	$smarty->assign('msg', $_SESSION['msg']);
	unset($_SESSION['msg']);
}

## Set active class variable for left menu
$smarty->assign('activeclass', 'leads');
$smarty->assign('mainmenu', '2');
## Unset all the objects created which are on this page

unset($codeObj);
$smarty->display(TEMPLATEDIR_ADMIN. 'manageleads/upload_codes.tpl');
unset($smarty);
?>